package com.talos.ums.controller.admin.appversion;

import com.talos.framework.apilog.core.annotation.ApiAccessLog;
import com.talos.framework.common.pojo.CommonResult;
import com.talos.framework.common.pojo.PageParam;
import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.framework.excel.core.util.ExcelUtils;
import com.talos.ums.controller.admin.appversion.vo.UmsAppVersionPageReqVO;
import com.talos.ums.controller.admin.appversion.vo.UmsAppVersionRespVO;
import com.talos.ums.controller.admin.appversion.vo.UmsAppVersionSaveReqVO;
import com.talos.ums.dal.dataobject.appversion.UmsAppVersionDO;
import com.talos.ums.service.appversion.UmsAppVersionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.talos.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.talos.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 移动应用版本管理")
@RestController
@RequestMapping("/ums/app-version")
@Validated
public class UmsAppVersionController {

    @Resource
    private UmsAppVersionService appVersionService;

    @PostMapping("/create")
    @Operation(summary = "创建移动应用版本管理")
    @PreAuthorize("@ss.hasPermission('ums:app-version:create')")
    public CommonResult<Long> createAppVersion(@Valid @RequestBody UmsAppVersionSaveReqVO createReqVO) {
        return success(appVersionService.createAppVersion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新移动应用版本管理")
    @PreAuthorize("@ss.hasPermission('ums:app-version:update')")
    public CommonResult<Boolean> updateAppVersion(@Valid @RequestBody UmsAppVersionSaveReqVO updateReqVO) {
        appVersionService.updateAppVersion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除移动应用版本管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ums:app-version:delete')")
    public CommonResult<Boolean> deleteAppVersion(@RequestParam("id") Long id) {
        appVersionService.deleteAppVersion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得移动应用版本管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ums:app-version:query')")
    public CommonResult<UmsAppVersionRespVO> getAppVersion(@RequestParam("id") Long id) {
        UmsAppVersionDO appVersion = appVersionService.getAppVersion(id);
        return success(BeanUtils.toBean(appVersion, UmsAppVersionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得移动应用版本管理分页")
    @PreAuthorize("@ss.hasPermission('ums:app-version:query')")
    public CommonResult<PageResult<UmsAppVersionRespVO>> getAppVersionPage(@Valid UmsAppVersionPageReqVO pageReqVO) {
        PageResult<UmsAppVersionDO> pageResult = appVersionService.getAppVersionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UmsAppVersionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出移动应用版本管理 Excel")
    @PreAuthorize("@ss.hasPermission('ums:app-version:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAppVersionExcel(@Valid UmsAppVersionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UmsAppVersionDO> list = appVersionService.getAppVersionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "移动应用版本管理.xls", "数据", UmsAppVersionRespVO.class,
                        BeanUtils.toBean(list, UmsAppVersionRespVO.class));
    }

}
